package com.wangjie.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangjie.order.domain.Order;
import com.wangjie.order.feign.AccountFeignClient;
import com.wangjie.order.service.OrderService;
import com.wangjie.order.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private AccountFeignClient accountFeignClient;

    @Transactional
    @Override
    public Order create(String userId, String commodityCode, int count) {
        // 1.计算订单价格
        int money = calculate(commodityCode, count);

        // 2.扣减账户余额
        accountFeignClient.debit(userId, money);

        // 3.保存订单
        Order order = new Order();
        order.setUserId(userId);
        order.setCommodityCode(commodityCode);
        order.setCount(count);
        order.setMoney(money);

        orderMapper.insert(order);

        int i = 10 / 0;

        return order;
    }

    // 计算价格
    private int calculate(String commodityCode, int count) {
        return 9 * count;
    }
}